package cn.edu.heuet.bnve.activity.SettingActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;

import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.databinding.ActivityTodoSettingBinding;
import cn.edu.heuet.bnve.utils.SharepreferencesUtilSystemSettings;

public class TodoSettingActivity extends AppCompatActivity {


    private ActivityTodoSettingBinding binding;
    private XUIGroupListView mGroupListView;
    private Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_todo_setting);
        initData();
        initView();
        initEvent();
    }

    public static void show(Context context) {
        context.startActivity(new Intent(context, TodoSettingActivity.class));
    }


    public void initData(){

    }

    public void initView(){
        mGroupListView = binding.todoGroupListView;
        initGroupListView();

        toolbar=binding.toolbarContainer.toolbar;
        setSupportActionBar(toolbar);
        toolbar.setTitle(R.string.setting);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void initEvent(){


    }
    private void initGroupListView() {

        XUICommonListItemView item1 = mGroupListView.createItemView(
                "周起始日");
        item1.setTag(1);
        item1.setOrientation(XUICommonListItemView.HORIZONTAL);
        item1.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        String[] item1Values ={"周日","周一","周六"};
        switch (SharepreferencesUtilSystemSettings.getValue(this,"calender_start_day",0)){
            case 0:
                item1.setDetailText(item1Values[0]);
                break;
            case 1:
                item1.setDetailText(item1Values[1]);
                break;
            case 2:
                item1.setDetailText(item1Values[2]);
                break;
        }

        XUICommonListItemView item2 = mGroupListView.createItemView(
                "月视图模式");
        item2.setTag(2);
        item2.setOrientation(XUICommonListItemView.HORIZONTAL);
        item2.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        String[] item2Values ={"全部显示","仅当前月份","自动填充"};
        switch (SharepreferencesUtilSystemSettings.getValue(this,"calender_month_view_model",0)){
            case 0:
                item2.setDetailText(item2Values[0]);
                break;
            case 1:
                item2.setDetailText(item2Values[1]);
                break;
            case 2:
                item2.setDetailText(item2Values[2]);
                break;
        }

        XUICommonListItemView item3 = mGroupListView.createItemView("日历可收缩");
        item3.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        int item3Value = SharepreferencesUtilSystemSettings.getValue(this,"calender_gesture_model",0);
        item3.getSwitch().setChecked(item3Value==0?true:false);
        item3.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"calender_gesture_model",0);
                }
                else{
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"calender_gesture_model",2);
                }
            }
        });

        XUICommonListItemView item4 = mGroupListView.createItemView("显示进度条");
        item4.setOrientation(XUICommonListItemView.VERTICAL);
        item4.setDetailText("日程完成进度会显示在日历上");
        item4.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        boolean item4Value = SharepreferencesUtilSystemSettings.getValue(this,"calender_progress_visible",true);
        item4.getSwitch().setChecked(item4Value);
        item4.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"calender_progress_visible",true);
                }
                else{
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"calender_progress_visible",false);
                }
            }
        });

        XUICommonListItemView item5 = mGroupListView.createItemView("日程标签动画");
        item5.setOrientation(XUICommonListItemView.VERTICAL);
        item5.setDetailText("日程标签旋转动画");
        item5.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        boolean item5Value = SharepreferencesUtilSystemSettings.getValue(this,"schedule_action",true);
        item5.getSwitch().setChecked(item5Value);
        item5.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"schedule_action",true);
                }
                else{
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"schedule_action",false);
                }
            }
        });
        XUICommonListItemView item6 = mGroupListView.createItemView("日程完成划线");
        item6.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        boolean item6Value = SharepreferencesUtilSystemSettings.getValue(this,"schedule_complete_strike",true);
        item6.getSwitch().setChecked(item6Value);
        item6.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"schedule_complete_strike",true);
                }
                else{
                    SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"schedule_complete_strike",false);
                }
            }
        });

        XUICommonListItemView item7 = mGroupListView.createItemView(
                "日程通知");
        item7.setTag(7);
        item7.setOrientation(XUICommonListItemView.VERTICAL);
        item7.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        item7.setDetailText("详见通知设置");


        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof XUICommonListItemView) {
                    int tagValue = (Integer) ((XUICommonListItemView) v).getTag();
                    switch (tagValue){
                        case 1:
                            new BottomSheet.BottomListSheetBuilder(TodoSettingActivity.this)
                                    .setTitle("周起始日")
                                    .addItem("周日")
                                    .addItem("周一")
                                    .addItem("周六")
                                    .setIsCenter(true)
                                    .setOnSheetItemClickListener((dialog, itemView, position, tag) -> {
                                        dialog.dismiss();
                                        SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"calender_start_day",position);
                                        item1.setDetailText(item1Values[position]);
                                    })
                                    .build()
                                    .show();
                            break;
                        case 2:
                            new BottomSheet.BottomListSheetBuilder(TodoSettingActivity.this)
                                    .setTitle("月视图模式")
                                    .addItem("全部显示")
                                    .addItem("仅当前月份")
                                    .addItem("自动填充")
                                    .setIsCenter(true)
                                    .setOnSheetItemClickListener((dialog, itemView, position, tag) -> {
                                        dialog.dismiss();
                                        SharepreferencesUtilSystemSettings.putValue(TodoSettingActivity.this,"calender_month_view_model",position);
                                        item2.setDetailText(item2Values[position]);
                                    })
                                    .build()
                                    .show();
                            break;
                        case 7:
                            NotifySettingActivity.show(TodoSettingActivity.this);
                            break;
                        default:
                            break;
                    }

                }
            }
        };

//        int size = DensityUtils.dp2px(this, 23);
        XUIGroupListView.newSection(this)
                .setTitle("日历设置")
                .addItemView(item1, onClickListener)
                .addItemView(item2, onClickListener)
                .addItemView(item3, onClickListener)
                .addItemView(item4, onClickListener)
                .addTo(mGroupListView);

        XUIGroupListView.newSection(this)
                .setTitle("日程设置")
                .addItemView(item5, onClickListener)
                .addItemView(item6, onClickListener)
                .addTo(mGroupListView);
    }
}